﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using LuxuryResort.Entity;

namespace LuxuryResort.DAO
{
    public class ReservationDAO : BaseDAO
    {
        public void Insert(Reservation reservation, Guid userId, Guid resortId)
        {
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    context.Reservations.AddObject(reservation);
                    context.SaveChanges();
                }
            }
        }

        public List<KeyValuePair<string, Reservation>> GetAll(Guid userId, Guid resortId)
        {
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            var lst = new List<KeyValuePair<string, Reservation>>();

            foreach (LRMPConnection connection in cs)
            {
                using (var context = new LRMPEntities(connection.ConnectionString))
                {
                    ObjectQuery<Reservation> reservations = context.Reservations.Include("Room");
                    foreach (Reservation r in reservations)
                    {
                        lst.Add(new KeyValuePair<string, Reservation>(connection.ResortName, r));
                    }
                }
            }

            return lst;
        }

        public List<Reservation> GetReservations(Guid userId, Guid resortId)
        {
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            var lst = new List<Reservation>();

            foreach (LRMPConnection connection in cs)
            {
                using (var context = new LRMPEntities(connection.ConnectionString))
                {
                    lst.AddRange(context.Reservations.Include("Room"));
                }
            }

            return lst;
        }

        public void DeleteReservation(Guid reservationId, Guid roomId, Guid userId, Guid resortId)
        {
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    Reservation reservation =
                        context.Reservations.FirstOrDefault(x => x.RSID == reservationId && x.RMID == roomId);
                    context.Reservations.DeleteObject(reservation);
                    context.SaveChanges();
                }
            }
        }

        public void UpdateReservation(Reservation reservation, Guid userId, Guid resortId)
        {
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);

            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    Reservation entity =
                        context.Reservations.FirstOrDefault(
                            x => x.RSID == reservation.RSID && x.RMID == reservation.RMID);

                    if (entity != null)
                    {
                        entity.CustomerName = reservation.CustomerName;
                        entity.Quantity = reservation.Quantity;
                        entity.CheckIn = reservation.CheckIn;
                        entity.CheckOut = reservation.CheckOut;
                        entity.PaymentMethod = reservation.PaymentMethod;
                        entity.PaymentDone = reservation.PaymentDone;
                        entity.Total = reservation.Total;
                        entity.Status = reservation.Status;

                        context.SaveChanges();
                    }
                }
            }
        }
    }
}